import common from '../common/common.js'
class request {
	constructor() {

	}

	/**
	 * GET类型的网络请求
	 */
	getRequest(url, data, header) {
		return this.requestAll(url, data, header, 'GET')
	}

	/**
	 * DELETE类型的网络请求
	 */
	deleteRequest(url, data, header) {
		return this.requestAll(url, data, header, 'DELETE')
	}

	/**
	 * PUT类型的网络请求
	 */
	putRequest(url, data, header) {
		return this.requestAll(url, data, header, 'PUT')
	}

	/**
	 * POST类型的网络请求
	 */
	postRequest(url, data, header) {
		return this.requestAll(url, data, header, 'POST')
	}

	/**
	 * 网络请求
	 */
	requestAll(url, data, header, method) {
		return new Promise((resolve, reject) => {
			uni.request({
				url,
				data,
				header,
				method,
				success: (res => {
					if (res.statusCode && res.statusCode == '404') {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}
					if (!res.data || !res.data.data) {
						resolve(res)
						return
					}
					const code = res.data.data.bussinessCode || ''
					if (res.data && (code == '401' || code == '119')) {
						uni.setStorage({
							key: 'TOKEN',
							data: ''
						});
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}
					resolve(res)
				}),
				fail: (res => {
					let showError = typeof res == 'object' ? JSON.stringify(res) : res
						.toString();
					reject(showError)
				})
			})
		})
	}
}

export default request